package com.search.service.impl;
import java.util.List;

import com.search.entity.CanBorrowBookVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.search.service.BorrowBookService;
import com.search.dao.BorrowBookDao;
import com.search.entity.BorrowBook;
/**
 * BorrowBook的服务接口的实现类
 * 
 * @author 
 *
 */
@Service
public class BorrowBookServiceImpl implements BorrowBookService {
	private final Logger LOG = LogManager.getLogger(this.getClass());

	@Autowired
	private BorrowBookDao borrowBookDao;
	// TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化
	private static final int C200 = 200;
	private static final int C412 = 412;
	public String resultFormat(int code, Object data) {
		JSONObject result = new JSONObject();
		result.put("code", code);
		if (data != null) {
			result.put("data", data);
		}
		return result.toJSONString();
	}

	@Override
	public List<BorrowBook> find(BorrowBook value) {
		//TODO这里可以做通过Assist做添加查询
		if(value.getOffset()==0){
			value.setOffset(10);
		}
		value.setHolding(1);
		List<BorrowBook> result = borrowBookDao.selectBorrowBookByObj(value);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行获取BorrowBook数据集-->结果:", result);
		}
		return result;
	}
	@Override
	public BorrowBook findOne(String id) {
		if (id == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行通过BorrowBook的id获得BorrowBook对象-->失败:id不能为空");
			}
			return null;
		}
		BorrowBook result = borrowBookDao.selectBorrowBookById(id);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行通过BorrowBook的id获得BorrowBook对象-->结果:", result);
		}
		return result;
	}

	@Override
	@Async
	public String saveNotNull(BorrowBook value) {
		if (value == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行将BorrowBook中属性值不为null的数据保存到数据库-->失败:对象不能为空");
			}
			return resultFormat(C412, null);
		}
		int result = borrowBookDao.insertNotNullBorrowBook(value);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行将BorrowBook中属性值不为null的数据保存到数据库-->结果:", result);
		}
		return resultFormat(C200, result);
	}

	@Override
	public int saveList(List<BorrowBook> list) {
		return borrowBookDao.insertBorrowBookByBatch(list);
	}

	@Override
	public int updateNotNullById(BorrowBook value) {
		if (value == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行通过BorrowBook的id更新BorrowBook中属性不为null的数据-->失败:对象为null");
			}
			return 0;
		}
		int result = borrowBookDao.updateNotNullBorrowBookById(value);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行通过BorrowBook的id更新BorrowBook中属性不为null的数据-->结果:", result);
		}
		return result;
	}

	@Override
	public String deleteById(String id) {
		if (id == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行通过BorrowBook的id删除BorrowBook-->失败:id不能为空");
			}
			return resultFormat(C412, null);
		}
		int result = borrowBookDao.deleteBorrowBookById(id);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行通过BorrowBook的id删除BorrowBook-->结果:", result);
		}
		return resultFormat(C200, result);
	}

	@Override
	public int count(BorrowBook enti) {
		enti.setHolding(1);
		return borrowBookDao.count( enti);
	}


}